<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>

        // 重构upload函数
        // 提供一个创建方法
        // 管理upload方法 1. add方法添加data 2. 设置状态
        // 开始函数
        class Upload {
            constructor(uploadType){
                this.uploadType = uploadType;
            }
            delFile (id) {
                uploadManager.setStatus(id,this)
                console.log(this);
                if(this.fileSize < 3000) {
                    return this.dom.parentNode.removeChild(this.dom)
                }
                if(window.confirm(`确定删除${this.fileName}文件吗？`)) {
                    return this.dom.parentNode.removeChild(this.dom)
                }
            }
        }
        // 创建upload对象工厂
        var createUploadFactory = (function() {
            var type = {}

            return {
                create: function (uploadType) {
                if(type[uploadType]){
                    return type[uploadType]
                }
                return type[uploadType] = new Upload(uploadType)
            }
            }
        })()
        // uploadManager
        var uploadManager = (function () {
            var dataBase = {}
            return {
                add: function (id,fileType,fileName,fileSize) {
                    var flyFile = createUploadFactory.create(fileType)
                    var dom = document.createElement("div")
                    dom.innerHTML = `<span>文件明:</span>${fileName}
                    <span>文件大小：</span>${fileSize}<button class="del">删除</button>`
                dom.querySelector(".del").onclick = function () {
                    console.log(id,"---");
                    flyFile.delFile(id)
                }
                document.body.appendChild(dom)
                dataBase[id] = {
                    fileName,
                    fileSize,
                    dom
                }
                return flyFile
                },
                setStatus: function (id,uploadFile) {
                    var file = dataBase[id]
                    for (const key in file) {
                        uploadFile[key] = file[key]
                    }
                }
            }
        })()

        var startUpload = (function () {
            var id = 0;

            return function (fileType , files) {
                for(let i = 0 , file; file = files[i++];){
                    uploadManager.add(id++, fileType,file.fileName,file.fileSize)
                }
            }
        })()
        startUpload( 'plugin', [
            {
              fileName: '1.txt',
              fileSize: 1000
            },
            {
              fileName: '2.html',
              fileSize: 3000
            },
            {
              fileName: '3.txt',
              fileSize: 5000
            }
        ]);

        startUpload( 'flash', [
            {
              fileName: '4.txt',
              fileSize: 1000
            },
            {
              fileName: '5.html',
              fileSize: 3000
            },
            {
              fileName: '6.txt',
              fileSize: 5000
          }
        ]);
    </script>
</body>
</html>